package com.example.note.repository;

import android.app.Application;
import androidx.lifecycle.LiveData;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.example.note.database.ReminderDao;
import com.example.note.database.ReminderEntity;
import com.example.note.database.NoteDatabase;

public class ReminderRepository {
    private ReminderDao reminderDao;
    private LiveData<List<ReminderEntity>> allReminders;
    private ExecutorService executor;
    
    public ReminderRepository(Application application) {
        NoteDatabase database = NoteDatabase.getInstance(application);
        reminderDao = database.reminderDao();
        allReminders = reminderDao.getAllReminders();
        executor = Executors.newFixedThreadPool(2);
    }
    
    public LiveData<List<ReminderEntity>> getAllReminders() {
        return allReminders;
    }
    
    public LiveData<ReminderEntity> getReminderById(int id) {
        return reminderDao.getReminderById(id);
    }
    
    public LiveData<List<ReminderEntity>> getActiveReminders() {
        return reminderDao.getActiveReminders();
    }
    
    public LiveData<List<ReminderEntity>> getCompletedReminders() {
        return reminderDao.getCompletedReminders();
    }
    
    public LiveData<Integer> getActiveReminderCount() {
        return reminderDao.getActiveReminderCount();
    }
    
    public void insertReminder(ReminderEntity reminder) {
        executor.execute(() -> reminderDao.insertReminder(reminder));
    }
    
    public void updateReminder(ReminderEntity reminder) {
        executor.execute(() -> reminderDao.updateReminder(reminder));
    }
    
    public void deleteReminder(ReminderEntity reminder) {
        executor.execute(() -> reminderDao.deleteReminder(reminder));
    }
    
    public void deleteReminderById(int id) {
        executor.execute(() -> reminderDao.deleteReminderById(id));
    }
    
    public void markReminderAsCompleted(int id) {
        executor.execute(() -> reminderDao.markReminderAsCompleted(id));
    }
    
    public List<ReminderEntity> getDueReminders(long currentTime) {
        return reminderDao.getDueReminders(currentTime);
    }
}